package cn.cangfei.massage.controller;


import cn.cangfei.massage.common.api.CommonResult;
import cn.cangfei.massage.dto.param.MassageProductParam;
import cn.cangfei.massage.dto.update.MassageProductDto;
import cn.cangfei.massage.dto.update.MassageProductFinalDto;
import cn.cangfei.massage.entity.MassageProduct;
import cn.cangfei.massage.service.IMassageProductService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;

/**
 * @author Lemon  2313812755@qq.com
 * @date 2023/2/5
 */

@Api(value = "产品模块", tags = "推拿馆产品接口")
@RestController
@CrossOrigin
@RequestMapping("/massage")
public class MassageProductController {

    @Resource
    private IMassageProductService massageProductService;

    @ApiOperation(value = "添加产品", notes = "传入 产品的对象")
    @RequestMapping(value = "/product", method = RequestMethod.POST)
    public CommonResult insertProduct(@RequestBody MassageProductParam param) {

        String name = param.getName();
        String price = param.getPrice();
        Long categoryId = param.getCategoryId();


        if (ObjectUtils.isEmpty(name) || ObjectUtils.isEmpty(price) || ObjectUtils.isEmpty(categoryId)) {
            return CommonResult.failed("请填写名称或上传图片或价格或分类");
        }

        if (massageProductService.count(name)) {
            return CommonResult.validateFailed("产品名称已存在！");
        }

        if (massageProductService.insertProduct(param)) {
            return CommonResult.success();
        }


        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    @ApiOperation(value = "更新产品信息", notes = "传入 产品编号、产品信息参数（名称，描述）")
    @ApiImplicitParam(name = "id", value = "产品编号", paramType = "path", dataType = "Long", required = true)
    @RequestMapping(value = "/product/{id}", method = RequestMethod.PUT)
    public CommonResult updateProduct(@PathVariable Long id, @RequestBody MassageProductParam param) {

        String name = param.getName();
        String price = param.getPrice();
        Long categoryId = param.getCategoryId();

        if (ObjectUtils.isEmpty(name) || ObjectUtils.isEmpty(price) || ObjectUtils.isEmpty(categoryId)) {
            return CommonResult.failed("请填写名称或价格或分类");
        }


        if (!massageProductService.count(id)) {
            return CommonResult.validateFailed("不存在，该产品! ");
        }


        if (massageProductService.update(id, param)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    @ApiOperation(value = "删除产品信息", notes = "传入 产品编号")
    @ApiImplicitParam(name = "id", value = "产品编号", paramType = "path", dataType = "Long", required = true)
    @RequestMapping(value = "/product/{id}", method = RequestMethod.DELETE)
    public CommonResult deleteProduct(@PathVariable Long id) {

        if (!massageProductService.count(id)) {
            return CommonResult.validateFailed("不存在，该产品编号! ");
        }

        if (massageProductService.delete(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    @ApiOperation(value = "获取产品", notes = "传入 产品编号")
    @ApiImplicitParam(name = "id", value = "产品编号", paramType = "path", dataType = "Long", required = true)
    @RequestMapping(value = "/product/{id}", method = RequestMethod.GET)
    public CommonResult<MassageProduct> getProduct(@PathVariable Long id) {

        if (!massageProductService.count(id)) {
            return CommonResult.validateFailed("不存在，该产品编号! ");
        }

        MassageProduct optionalProduct = massageProductService.getProduct(id);

        return CommonResult.success(optionalProduct);
    }

    @ApiOperation(value = "上下架", notes = "传入 产品编号, 状态")
    @RequestMapping(value = "/updateProductStatus", method = RequestMethod.GET)
    public CommonResult<MassageProduct> updateProductStatus(@RequestParam("id") Long id, @RequestParam("status") Integer status) {

        if (!massageProductService.count(id)) {
            return CommonResult.validateFailed("不存在，该产品编号! ");
        }

        Boolean result = massageProductService.updateProductStatus(id, status);

        if (result) {
            return CommonResult.success();

        }
        return CommonResult.failed("修改状态失败");

    }


    @ApiOperation(value = "分页：搜索产品信息", notes = "传入 产品名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "产品名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "第几页", paramType = "query", dataType = "Integer",
                    required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer",
                    required = true),
            @ApiImplicitParam(name = "up", value = "上架状态", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "categoryId", value = "分类编号", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "categoryName", value = "分类名称", paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/product/list", method = RequestMethod.GET)
    public CommonResult<IPage<MassageProductFinalDto>> searchProduct(@RequestParam(required = false) String name,
                                                                     @RequestParam(required = false) String up,
                                                                     @RequestParam(required = false) Long categoryId,
                                                                     @RequestParam(required = false) String categoryName,
                                                                     @RequestParam("pageNum") Integer pageNum,
                                                                     @RequestParam("pageSize") Integer pageSize) {

//        IPage<MassageProduct> list = massageProductService.list(name, up, categoryName, categoryId, pageNum, pageSize);
        IPage<MassageProductFinalDto> list = massageProductService.list(name, up, categoryName, categoryId, pageNum, pageSize);

        return CommonResult.success(list);
    }

    @ApiOperation("定时扫描产品过期")
    @RequestMapping(value = "/product/expiration", method = RequestMethod.GET)
    public CommonResult productExpiration() {
        massageProductService.productExpiration();
        return CommonResult.success();
    }

    @ApiOperation(value = "分页：搜索产品信息(小程序端)判断上架，并且在时间段内", notes = "传入 产品名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "产品名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "第几页", paramType = "query", dataType = "Integer",
                    required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer",
                    required = true),
    })
    @RequestMapping(value = "/product/app/list", method = RequestMethod.GET)
    public CommonResult<IPage<MassageProduct>> searchProductByApp(@RequestParam(required = false) String name,
                                                                   @RequestParam(required = false) Long categoryId,
                                                                   @RequestParam(required = false) String categoryName,
                                                                   @RequestParam Integer pageNum,
                                                                   @RequestParam Integer pageSize) {

        return CommonResult.success(massageProductService.listForApp(name, categoryName, categoryId, pageNum, pageSize));
    }


    @ApiOperation(value = "分页：搜索全部产品信息(小程序端)", notes = "传入 产品名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "分类id", paramType = "query", dataType = "Long",
                    required = true),
            @ApiImplicitParam(name = "pageNum", value = "第几页", paramType = "query", dataType = "Integer",
                    required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer",
                    required = true),
    })
    @RequestMapping(value = "/product/app/searchList", method = RequestMethod.GET)
    public CommonResult<IPage<MassageProductDto>> searchProductListByApp(
            Principal principal,
            @RequestParam(required = false) Long categoryId,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize) {
        // 这个是openId
        String name = principal.getName();

        return CommonResult.success(massageProductService.listForAppList(name, categoryId, pageNum, pageSize));
    }


}
